package com.mxsoft.takeaway.util;

import com.google.common.collect.Maps;
import lombok.Data;

/**
 * 前端响应类
 *
 * @param <T>
 */
@Data
public class HttpResult<T> {
    /**
     * 成功返回状态码
     */
    public static final Integer SUCCESS_STATUS = 1;
    /**
     * 失败返回状态码
     */
    public static final Integer FAILED_STATUS = 0;

    private T data;
    private Integer status;

    public HttpResult() {
    }

    public static <T> Builder<T> builder() {
        return new Builder<T>();
    }

    @Data
    public static class ErrorData {
        public static final Integer SUCCESS = 200;
        public static final Integer BAD_REQUEST = 400;
        public static final Integer UNAUTHORIZED = 401;
        public static final Integer FORBIDDEN = 403;
        public static final Integer NOT_FOUND = 404;
        public static final Integer FAILED = 500;
        private Integer errorCode = FAILED;
        private String message = "系统错误,请重试";

        public ErrorData() {

        }

        public ErrorData(Integer errorCode, String message) {
            this.errorCode = errorCode;
            this.message = message;
        }
    }

    public static class Builder<T> {
        private T data = (T) Maps.newHashMap();
        private Integer status = SUCCESS_STATUS;

        public Builder() {
        }

        public HttpResult build() {
            HttpResult<T> result = new HttpResult<T>();
            result.setData(data);
            result.setStatus(status);
            return result;
        }

        public Builder data(T data) {
            this.data = data;
            return this;
        }

        public Builder data(T data, Integer status, String message) {
            this.data = data;
            this.status = status;
//            this.message = message;
            return this;
        }

        public Builder failed() {
            this.status = FAILED_STATUS;
            this.data = (T) new ErrorData();
            return this;
        }

        public Builder failed(String message) {
            this.status = FAILED_STATUS;
            this.data = (T) new ErrorData(ErrorData.FAILED, message);
            return this;
        }

        public Builder failed(Integer code, String message) {
            this.status = FAILED_STATUS;
            this.data = (T) new ErrorData(code, message);
            return this;
        }

        public Builder timeout() {
            this.status = FAILED_STATUS;
            this.data = (T) new ErrorData(ErrorData.FAILED, "请求超时,请稍后重试");
            return this;
        }

        public Builder timeout(String message) {
            this.status = FAILED_STATUS;
            this.data = (T) new ErrorData(ErrorData.FAILED, message);
            return this;
        }


        public Builder success() {
            this.status = SUCCESS_STATUS;
            return this;
        }

        public Builder success(String message) {
            this.status = SUCCESS_STATUS;
//            this.message = message;
            return this;
        }
    }
}
